//
//  HomeViewController.swift
//  CopyWB
//
//  Created by imac on 2017/5/12.
//  Copyright © 2017年 iimac. All rights reserved.
//

import UIKit
private let cellId = "cellId"
class HomeViewController: BaseViewController {
    
    lazy var statusList = [String]()
    
    override func loadData() {
        
        NetworkManager.shared.statuesList { (statusList, isSuccessed) in
            if isSuccessed{
                print(statusList!)
            }
        }
        
        
        print("开始刷新\(NetworkManager.shared)")
        //模拟‘延时’加载数据   ->  dispath_after
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            
            for i in 0..<25{
                if self.isPullUp{
                    //上拉刷新追加到底部
                    self.statusList.append("上拉\(i)")
                }else{
                    //下拉刷新插入到数组顶部
                self.statusList.insert(i.description, at: 0)
                }
            }
            print("刷新表格")
            self.tableView?.reloadData()
            self.refreshController?.endRefreshing()
            self.isPullUp = false
        }
    }
    func showFriends(){
        
        let demoVC = DemoViewController()
        demoVC.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(demoVC, animated: true)
    }
    
}
// MARK: - 表格数据源方法
extension HomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = statusList[indexPath.row]
        
        return cell
    }
    
}

//MARK :     -设置界面
extension HomeViewController{
    override func setupTableView() {
        super.setupTableView()

        //  设置掉行蓝按钮方法
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriends))
        //  注册原型 cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        loadData()
        tableView?.delegate = self
        tableView?.dataSource = self
    }
}
